package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.BannerdataMapper;
import org.csu.jpetmanage.mapper.ProfileMapper;
import org.csu.jpetmanage.model.Bannerdata;
import org.csu.jpetmanage.model.Profile;
import org.csu.jpetmanage.service.BannerdataService;
import org.csu.jpetmanage.service.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping(value = "/Item")
public class ProfileController {
    @Autowired
    ProfileService profileService;



    @Autowired
    Profile profile;

    @Autowired
    ProfileMapper profileMapper;

    @Autowired
    BannerdataService bannerdataService;



    @Autowired
    Bannerdata bannerdata;

    @Autowired
    BannerdataMapper bannerdataMapper;

    String message = null;

    @GetMapping("/displayProfile")
    public String displayProfile( Model model){
        List<Profile> list =profileService.list();
        model.addAttribute("profileList" ,list);

        List<Bannerdata> bannerdatalist =bannerdataService.list();
        model.addAttribute("bannerdataList" ,bannerdatalist);
//        for(int i=0;i< list.size();i++){
//
//        }


        return "yuan/profile";
    }



    @RequestMapping("/addProfile")
    public String addProfile(
            @RequestParam("userid") String userid,
            @RequestParam("la") String la,
            @RequestParam("fa") String fa,
            Model model,
            HttpSession session){
        List<Profile> list =profileService.list();
        model.addAttribute("profileList" ,list);
        profile.setUserid(userid);
        profile.setLa(la);
        profile.setFa(fa);
        profileMapper.insert(profile);
        session.setAttribute("msg","插入成功");
        return "redirect:/displayProfile";
    }

    @RequestMapping("/deleteProfile")
    public String deleteProfile(
            @RequestParam("userid") String userid,
            Model model,
            HttpSession session
    ){
        List<Profile> list =profileService.list();
        model.addAttribute("profileList" ,list);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("userid",userid);
        int sum = profileMapper.deleteByMap(hashMap);
        if (sum>=1){
            session.setAttribute("msg","删除成功");
        }
        return "redirect:/displayProfile";
    }

    @GetMapping("/profileEditAjax")
    @ResponseBody
    public Map<String,Object> profileEditAjax(
            @RequestParam("userid") String userid,
            HttpSession session
    ){
        QueryWrapper<Profile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        List<Profile> profileList = profileMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (profileList.size()==1){
            profile = profileList.get(0);
            session.setAttribute("updateProfile",profile);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("userid",profile.getUserid());
            map.put("la",profile.getLa());
            map.put("fa",profile.getFa());
            return map;
        }
        return map;
    }

    @RequestMapping("/updateProfile")
    public String updateProfile(
            @RequestParam("userid") String userid,
            @RequestParam("la") String la,
            @RequestParam("fa") String fa,
            Model model,
            HttpSession session) {
        profile = (Profile) session.getAttribute("updateProfile");
        profile.setUserid(userid);
        profile.setLa(la);
        profile.setFa(fa);
        UpdateWrapper<Profile> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userid",profile.getUserid());
        int rows = profileMapper.update(profile,updateWrapper);
        session.setAttribute("msg","修改成功");
        return "redirect:/displayItem";
    }

    @RequestMapping("/searchProfile")
    public String searchProfile(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<Profile> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userid",keyword).
                or().like("la",keyword).
                or().like("fa",keyword);
        List<Profile> profileList=profileMapper.selectList(queryWrapper);
        model.addAttribute("profileList" ,profileList);
        return "yuan/profile";
    }

    @GetMapping("/profileSearchAjax")
    @ResponseBody
    public List<Profile> profileSearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        QueryWrapper<Profile> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userid",keyword).
                or().like("la",keyword).
                or().like("fa",keyword);
        return profileMapper.selectList(queryWrapper);
    }
}

